package com.face.server.dao;

import com.face.server.entity.DormitoryAdmin;
import com.face.server.entity.DormitoryBuild;
import com.face.server.entity.Student;
import com.face.server.vo.DormAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

import static org.springframework.data.mongodb.core.query.Criteria.where;

/**
 * Created by ryan on 2018/3/26.
 */
@Repository
public class DormAdminDao {
    @Autowired
    MongoTemplate mongoTemplate;


    public List<DormitoryAdmin> findAll(Pageable pageRequest) {
        Aggregation aggregation=Aggregation.newAggregation(
                Aggregation.match(new Criteria("type").is(2)),
                Aggregation.skip((long)pageRequest.getOffset()),
                Aggregation.limit(pageRequest.getPageSize())
        );
        return mongoTemplate.aggregate(aggregation,"joined",DormitoryAdmin.class).getMappedResults();
    }

    public List<DormitoryAdmin> findByDormitoryBuildNumIn(List<Integer> dormitoryNumList, Pageable pageRequest) {
        Aggregation aggregation=Aggregation.newAggregation(
                Aggregation.match(new Criteria().andOperator(new Criteria("type").is(2),new Criteria("dormitoryBuildNum").in(dormitoryNumList))),
                Aggregation.skip((long)pageRequest.getOffset()),
                Aggregation.limit(pageRequest.getPageSize())
        );
        return mongoTemplate.aggregate(aggregation,"joined",DormitoryAdmin.class).getMappedResults();
    }

    public int update(DormitoryAdmin dormitoryAdmin) {
        DormitoryBuild dormitoryBuild=mongoTemplate.findOne(Query.query(
                where("buildingNum").is(dormitoryAdmin.getDormitoryBuildNum())
        ),DormitoryBuild.class,"dormitoryBuild");
        dormitoryAdmin.setDormitoryBuild(dormitoryBuild);
        return mongoTemplate.updateFirst(Query.query(
                where("_id").is(dormitoryAdmin.getId())
                ), new Update()
                        .set("jobName", dormitoryAdmin.getJobName())
                        .set("phone", dormitoryAdmin.getPhone())
                        .set("identity", dormitoryAdmin.getIdentity())
                        .set("effectDate", dormitoryAdmin.getEffectDate())
                        .set("endDate", dormitoryAdmin.getEndDate())
                        .set("dormitoryNum", dormitoryAdmin.getDormitoryBuildNum())
                        .set("sex", dormitoryAdmin.isSex())
                        .set("name", dormitoryAdmin.getName())
                        .set("name", dormitoryAdmin.getName())
                        .set("dormitoryBuild",dormitoryAdmin.getDormitoryBuild()),
                "joined"
        ).getN();
    }

    public int delete(String id) {
        return mongoTemplate.remove(Query.query(
                where("_id").is(id)
        ),DormitoryAdmin.class,"joined").getN();
    }

    public int create(DormitoryAdmin dormAdmin) {
       mongoTemplate.insert(dormAdmin,"joined");
       return 1;
    }

    public DormitoryAdmin findOne(String id) {
        return mongoTemplate.findById(id, DormitoryAdmin.class, "joined");
    }
}
